const {
  addSalesMen,
  getSalesMen,
  delSalesMen,
  getSalesMenById,
  updateSalesMen,
} = require("../dao/salesMenDao");

//图片处理包
const { moveFiles, removeFiles } = require("../utils/handleFiles");

//获取销售员数据
module.exports.getSalesMen = async (params) => {
  const data = await getSalesMen(params);
  return {
    message: "销售员数据获取成功",
    status: 1,
    data,
  };
};

//新增销售员数据和头像
module.exports.addSalesMen = async (params) => {
  const data = await addSalesMen(params);
  //
  if (data._id) {
    // //移动图片
    moveFiles({
      fromPath: "./public/temp",
      toPath: "./public/images",
      filename: params.images,
    });
    // //删除缓存图片
    removeFiles("./public/temp");
    return {
      message: "销售员新增成功",
      status: 1,
      data,
    };
  } else {
    return {
      message: "销售员添加失败",
      status: 0,
    };
  }
};

// 删除销售员
module.exports.delSalesMen = async (params) => {
  const data = await delSalesMen(params);
  if (data.nModified > 0) {
    return {
      message: "删除成功",
      status: 1,
    };
  } else {
    return {
      message: "删除失败",
      status: 0,
    };
  }
};

// 通过id获取单个销售老哥数据
module.exports.getSalesMenById = async (params) => {
  const data = await getSalesMenById(params);
  if (data.length == 1) {
    return {
      message: "销售老哥数据获取成功",
      status: 1,
      data,
    };
  } else {
    return {
      message: "销售老哥数据获取失败",
      status: 0,
      data,
    };
  }
};

// 修改销售员
module.exports.updateSalesMen = async (params) => {
  const data = await updateSalesMen(params);
  if (data.nModified > 0) {
    return {
      message: "数据修改成功",
      status: 1,
    };
  } else {
    return {
      message: "学生数据修改失败",
      status: 0,
    };
  }
};
